import type { Profile } from "@/types/user";
import { getLocalToken, removeLocalToken, setLocalToken } from "@/utils/auth";
import { defineStore } from "pinia";
import { ref } from "vue";

// 全局状态管理用户模块
export const useUserStore = defineStore("hm-user", () => {
    // token 信息
  const token = ref(getLocalToken() || "");

  //登录成功， 保存 token
  const setToken = (val: string) => {
    // 存储 token 到 store 中，内存访问速度更快
    token.value = val;
    // 持久化存储
    setLocalToken(val);
  };

  // 退出登录，删除token
  const clearToken = () => {
    token.value = "";
    // 删除持久化
    removeLocalToken();
  };

  //用户信息
  const profile = ref<Profile>({} as Profile);
  //设置用户信息
  const setProfile = (val: Profile) => {
    profile.value = val;
  };
  //清除用户信息
  const clearProfile = () => {
    profile.value = {} as Profile;
  };

  // 记得在最后 return 出去
  return {
    profile,
    setProfile,
    clearProfile,
    token,
    setToken,
    clearToken,
  };
});
